<?php
/**
 * Project: firework-cms             >>  http://firework-cms.googlecode.com
 * Authors: YouniS Ben Salah  (-.-)  >>  http://younishd.kegtux.org
 *          Fabien Wang       (^.^)  >>  http://fabienwang.fr
 * License: See README and LICENSE in the top level directory
 */
 
/**
 * Module: Config [CFG]
 * All values are set at runtime by controller using config models.
 */
 
require_once 'core/cl_view.php';

// define directory separator
define( 'DS', DIRECTORY_SEPARATOR );

/**
 * Class: APP_CONFIG
 * static class, handles the general configuration of the webapp, debug mode settings etc.
 */
class APP_CONFIG
{
    // debug mode level
    public static $DEBUG_LVL;
    
    // write log flag
    public static $WRITE_LOG;
    
    // log file
    public static $LOG_FILE;
    
    // default page or home page
    public static $DEFAULT_ACTION;
    
    // number of last recent posts to show on blog
    public static $NB_RECENT_POSTS;
    
    // 404 page not found error message
    public static $ERRMSG_404;

    /**
     * Method: LOG_DEBUG()
     * write debug message to log file.
     * looks like this:
     *      [ERR][13/07/2012@23:21:48]   something went terribly wrong.
     */
    public static function LOG_DEBUG( $type, $msg )
    {
        if ( APP_CONFIG::$WRITE_LOG ) {
            $prefix = ( $type == 'exception' ? '[ERR]' : '[DBG]' );
            $prefix .= date( '[d/m/Y@H:i:s]' );
            $logfile = fopen( APP_CONFIG::$LOG_FILE, 'ab' );
            fwrite( $logfile, $prefix.'   '.$msg."\n" );
        }
    }

    /**
     * Method: DISPLAY_DEBUG()
     * display a styled debug message while handling message types like debug and exception.
     */
    public static function DISPLAY_DEBUG( $type, $msg )
    {
        if ( self::$DEBUG_LVL == 1 ) {
            if ( $type == 'exception' ) {
                $debug_view = new View( $type );
                $debug_view->addData( 'msg', $msg );
                die( $debug_view->genCode() );
            }
            else if ( $type != 'debug' ) {
                die( 'error: in static method DISPLAY_DEBUG() in class APP_CONFIG. check passed argument "type".' );
            }
        }
        else if ( self::$DEBUG_LVL == 2 ) {
            if ( $type == 'exception' || $type == 'debug' ) {
                $debug_view = new View( $type );
                if ( is_array($msg) ) {
                    // output buffering to get var_dump() as string
                    ob_start();
                    var_dump($msg);
                    $var_dump_str = ob_get_contents();
                    ob_end_clean();
                    $debug_view->addData( 'msg', $var_dump_str );
                }
                else {
                    $debug_view->addData( 'msg', $msg );
                }
                // only die if exception
                if ( $type != 'exception' ) {
                    echo $debug_view->genCode();
                }
                else {
                    die( $debug_view->genCode() );
                }
            }
            else {
                die( 'error: in static method DISPLAY_DEBUG() in class APP_CONFIG. check passed argument "type".' );
            }
        }
        else if ( self::$DEBUG_LVL ) {
            die( 'error: in static method DISPLAY_DEBUG() in class APP_CONFIG. check "DEBUG_LVL".' );
        }
    }
}

/**
 * Class: DB_CONFIG
 * static class, contains preferences like db connection parameters and static method to connect to db.
 */
class DB_CONFIG
{
    // db config array
    public static $DB_SETTINGS = array();
    
    /**
     * Method: CONNECT()
     * connects to database using PDO and returns connection object ( PDO obj ) by ref.
     */
    public static function &CONNECT()
    {
        // check if already connected to db
        if ( self::$CONNECTED != NULL ) {
            return self::$CONNECTED;
        }
        else {
            // establish new connection
            $pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
            $pdo_options[PDO::ATTR_DEFAULT_FETCH_MODE] = PDO::FETCH_ASSOC;
            $pdo_options[PDO::MYSQL_ATTR_INIT_COMMAND] = "SET NAMES utf8";
            if ( isset( self::$DB_SETTINGS['ENGINE'] ) && isset( self::$DB_SETTINGS['HOST'] ) && isset( self::$DB_SETTINGS['NAME'] )
                    && isset( self::$DB_SETTINGS['USER'] ) && isset( self::$DB_SETTINGS['PWD'] ) ) {
                self::$CONNECTED = new PDO( self::$DB_SETTINGS['ENGINE'] . ':host=' . self::$DB_SETTINGS['HOST'] . ';dbname=' . self::$DB_SETTINGS['NAME'],
                        self::$DB_SETTINGS['USER'], self::$DB_SETTINGS['PWD'], $pdo_options );
                return self::$CONNECTED;
            }
            else {
                throw new Exception( 'can\'t connect to db. check DB_CONFIG you noob!' );
            }
        }
    }
    
    // current cnx to db.
    public static $CONNECTED = NULL;
}

/**
 * Class: TEMPLATE_CONFIG
 */
class TEMPLATE_CONFIG
{
    public static $DEFAULT_TEMPLATE;
}
